package com.web.servlet.user;


import com.web.bean.user.User;
import com.web.dao.user.UserDao;
import com.web.dao.user.UserDaoImpl;
import com.web.result.Result;
import com.web.service.user.UserService;
import com.web.service.user.UserServiceImpl;
import com.web.utils.DBUtil;
import com.web.utils.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @autor Chen
 * @create 2021--25-9:02
 * @desc
 */

/**
 * 查询所有用户 ：/user/selectUser
 * 新增读者 : /user/addUser User
 * 新增作者 : /user/addWriter wrtier
 * 修改用户 :/user/updateUser?uid=1 User
 * 修改作者 : /user/updateWriter?uid=? writer
 * 删除用户: /user/deleteUser?id=1
 * 根据电话号码查询作者 :/user/selectWriterByPhone?uid=?
 */
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String uri = req.getRequestURI().replace("/user/", "");

        if ("selectUser".equals(uri)){
            selectUser(req,resp);
        }
        if ("addUser".equals(uri)){
            User user = new User();
            user.setUid(req.getParameter("uid"));
            user.setUname(req.getParameter("uname"));
            user.setUpwd(req.getParameter("upwd"));
            addUser(req,resp,user);
        }
        if ("updateUser".equals(uri)){
            User user = new User();
            user.setUid(req.getParameter("uid"));
            user.setUname(req.getParameter("uname"));
            updateUser(req,resp,user);
        }
        if ("deleteUser".equals(uri)){
            String uid = req.getParameter("uid");
            deleteUser(req,resp,uid);
        }
        if ("selectUserByPhone".equals(uri)){
            String phone = req.getParameter("phone");
            selectUserByPhone(req,resp,phone);
        }
        if ("addWriter".equals(uri)){
            User user = new User();
            user.setWname(req.getParameter("wname"));
            user.setWpwd(req.getParameter("wpwd"));
            user.setIdnum(req.getParameter("idnum"));
            addWriter(req,resp,user);
        }
        if ("updateWriter".equals(uri)){
            User user = new User();
            user.setWname(req.getParameter("wname"));
            user.setWpwd(req.getParameter("wpwd"));
            user.setIdnum(req.getParameter("idnum"));
            updateWriter(req,resp,user);
        }
        if ("selectWriterByPhone".equals(uri)){
            String phone = req.getParameter("phone");
            selectWriterByPhone(req,resp,phone);
        }

    }
    /*
     *根据phone查询作者
     */
    private void selectWriterByPhone(HttpServletRequest req, HttpServletResponse resp, String phone) throws  ServletException,IOException {
        DBUtil dbUtil = DBUtil.getInstance();
        String writerphone = req.getParameter(phone);
        Result result = new Result();
        try {
            UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
            User user = userDao.selectWriterByPhone(writerphone);
            req.setAttribute("writer", user);
            result.setFlag(true);
            result.setMessage("获取成功");
        }catch (Exception e) {
             e.printStackTrace();
             result.setFlag(false);
             result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/writer.jsp").forward(req,resp);
        }
    }
    private void updateWriter(HttpServletRequest req, HttpServletResponse resp, User user)throws  ServletException,IOException {
        String wname=req.getParameter("uname");
        String wpwd=req.getParameter("wpwd");
        String idnum=req.getParameter("idnum");
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
             UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
             userDao.updateWriter(user);
             user.setWname(wname);
             user.setWpwd(wpwd);
             user.setIdnum(idnum);
             result.setFlag(true);
             result.setMessage("修改成功");
         }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
         }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/writerRegister.jsp").forward(req,resp);
        }
    }


    private void addWriter(HttpServletRequest req, HttpServletResponse resp, User user) throws  ServletException,IOException{
        String wname = req.getParameter("wname");
        String wpwd = req.getParameter("wpwd");
        String idnum = req.getParameter("idnum");
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
            UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
            user = userDao.selectWriterByPhone("phone");
            user.setWname(wname);
            user.setWpwd(wpwd);
            user.setIdnum(idnum);
            result.setFlag(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/writer.jsp").forward(req,resp);
        }

    }

    private void selectUserByPhone(HttpServletRequest req, HttpServletResponse resp, String phone) throws  ServletException,IOException{
        DBUtil dbUtil = DBUtil.getInstance();
        String userphone = req.getParameter(phone);
        Result result = new Result();
        try {
            UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
            User user = userDao.selectUserByPhone(userphone);
            req.setAttribute("writer", user);
            result.setFlag(true);
            result.setMessage("获取成功");
        }catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/writer.jsp").forward(req,resp);
        }
    }

    private void deleteUser(HttpServletRequest req, HttpServletResponse resp, String uid)throws  ServletException,IOException {
        Result result = new Result();
        try {
            UserServiceImpl userService = new UserServiceImpl();
            userService.deleteUser(uid);
            result.setFlag(true);
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            req.getRequestDispatcher("/updateUser.jsp").forward(req,resp);
        }

    }

    private void updateUser(HttpServletRequest req, HttpServletResponse resp, User user) throws  ServletException,IOException{
        String uname=req.getParameter("uname");
        String upwd=req.getParameter("upwd");
        String phone=req.getParameter("phone");
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
            UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
            userDao.updateWriter(user);
            user.setWname(uname);
            user.setWpwd(upwd);
            user.setPhone(phone);
            result.setFlag(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/user.jsp").forward(req,resp);
        }

    }

    /*
        pst = conn.prepareStatement(sql);
        pst.setString(1,user.getUid());
        pst.setString(2,user.getUname());
        pst.setString(3,user.getUpwd());
        pst.setString(4,user.getPhone());
        pst.setTimestamp(5,user.getUdate());
        pst.setString(6,user.getIsverify());
        pst.setInt(7,user.getUstatus());
     */
    private void addUser(HttpServletRequest req, HttpServletResponse resp, User user) throws  ServletException,IOException{
        String uid = req.getParameter("uid");
        String uname = req.getParameter("wname");
        String upwd = req.getParameter("wpwd");
        String phone = req.getParameter("phone");
        Timestamp udate = Timestamp.valueOf(req.getParameter("Udate"));
        String isverify = req.getParameter("isverify");
        Result result = new Result();
        DBUtil dbUtil = DBUtil.getInstance();
        try {
            UserDaoImpl userDao = new UserDaoImpl(dbUtil.getConnection());
            User user1 = userDao.selectWriterByPhone("phone");
            user1.setUid(uid);
            user1.setWname(uname);
            user1.setWpwd(upwd);
            user1.setPhone(phone);
            user1.setUdate(udate);
            user1.setIsverify(isverify);
            result.setFlag(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/user.jsp").forward(req,resp);
        }

    }

    private void selectUser(HttpServletRequest req, HttpServletResponse resp) throws  ServletException,IOException{
        DBUtil dbUtil = DBUtil.getInstance();
        Result result = new Result();
        String uid = req.getParameter("uid");
        try {
            UserServiceImpl service = new UserServiceImpl();
            List<User> users = service.selectUsers();
            result.setFlag(true);
            result.setMessage("获取成功");
            result.setData(users);
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("/user.jsp").forward(req,resp);
        }
    }

}

